﻿@*仓库模块-物品出库*@
@using A2.ERP.Models.ViewModels
<style type="text/css">
    /*头部*/
    #top {
        margin-bottom: 20px;
    }
        /*文本只有下划线*/
        #top input[type='text'], #BuyOrder input[type='date'], #BuyOrder input[type='number'] {
            border: 0px;
            border-bottom: 1px solid black;
        }
    /*尾部*/
    #bottom {
        width: 100%;
        height: 40%;
        border: 1px solid #ffd800;
    }

    .table_none {
        display: none;
    }

    #bottom tr th {
        text-align: center;
    }

    #ol tr td {
        text-align: center;
    }

    pre {
        display: inline-block;
        margin-bottom: 0;
    }
</style>
<section id="OutStackModule">
    <div id="top">
        <div class="card">
            <div class="card-header">
                商品出库
            </div>
            <div class="card-body">
                @using (Html.BeginForm("AddAppendStockModule", "Store", FormMethod.Post, new
                {
                    @class = "addGroupFrm"
                }))
                {
                    <p>
                        订单编号：<input type="text" id="AppendID" name="AppendID" />&nbsp;&nbsp;&nbsp;&nbsp;
                        制单日期：<input type="date" ID="CreateDate" name="CreateDate" />&nbsp;&nbsp;&nbsp;&nbsp;
                        入库类型：
                        <select class="custom-select" iID="AppendType" name="AppendType" style="width: 150px;">
                            <option value="0">报溢</option>
                            <option value="1">借入</option>
                            <option value="2">借出还入</option>
                            <option value="3">受赠</option>
                            <option value="4">其他入仓</option>
                        </select>
                    </p>
                    <p>
                        <pre>库     房：</pre>
                        <select class="custom-select" ID="StoreHouseID" name="StoreHouseID" style="width: 150px;">
                            @foreach (var item3 in ViewBag.typeList3)
                            {
                                <option value="@item3.HouseID">@item3.HouseName</option>
                            }
                        </select>
                        &nbsp;&nbsp;&nbsp;&nbsp;库&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;区：
                        &nbsp;&nbsp;
                        <select class="custom-select" ID="HouseDetailID" name="HouseDetailID" style="width: 150px;">
                            @foreach (var item2 in ViewBag.typeList2)
                            {
                                <option value="@item2.HouseID">@item2.SubareaName</option>
                            }
                        </select>
                        &nbsp;&nbsp;&nbsp;&nbsp;入库日期：<input type="date" ID="TradeDate" name="TradeDate" />
                    </p>
                    <p>已经收款：<input type="number" ID="AlreadyPay" name="AlreadyPay" /></p>
                    <p>
                        备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:<input type="text" id="Description" name="Description" style="width: 500px;" />
                        &nbsp;&nbsp;
                        <button type="button" id="addtable" class="btn btn-success">
                            添加表单
                        </button>
                        <button type="button" id="addetable" class="btn btn-success ">
                            提交表单
                        </button>
                    </p>
                }
            </div>
        </div>
    </div>
    <div id="tables" class="card table_none">
        <div class="card-header">
            表单信息
        </div>
        <div class="card-body">
            <form class="updateUserFrm" method="put" action="/Repertory/ADDEitity">

                <div class="form-row">
                    <div class="form-group col-md-6">
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <label class="input-group-text" for="inputGroupSelect01">商品</label>
                            </div>
                            <select class="custom-select" id="ProductsName"></select>
                        </div>
                    </div>
                    <div class="form-group col-md-6">
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="basic-addon1">价格</span>
                            </div>
                            <input type="number" id="Price" name="Price" class="form-control" aria-label="Username" aria-describedby="basic-addon1">
                        </div>
                    </div>
                </div>
                <div class="form-row">
                    <div class="form-group col-md-6">
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="basic-addon1">数量</span>
                            </div>
                            <input type="number" id="Quantity" name="TotalSalesNum" class="form-control" aria-label="Username" aria-describedby="basic-addon1">
                        </div>
                    </div>
                    <div class="form-group col-md-6">
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="basic-addon1">供应商</span>
                            </div>
                            <select class="custom-select" id="SupplierName" name="SupplierName"></select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-prepend">
                            <span class="input-group-text">描述</span>
                        </div>
                        <textarea name="ProductsName" id="Descriptions" class="form-control" aria-label="With textarea"></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <button type="button" id="baocun" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
    </div>
    @*尾部*@
    <div id="bottom">
        <table class="table table-bordered">
            <thread>
                <tr>
                    <th>商品编号</th>
                    <th>商品名称</th>
                    <th>供应商</th>
                    <th>数量</th>
                    <th>采购额</th>
                    <th>金额</th>
                    <th>操作</th>
                </tr>
            </thread>
            <tbody id="ol"></tbody>
        </table>
    </div>
    <a onclick="retemet()"></a>
    @*增加*@
    <div class="modal fade" id="AddAppendStockInfoModuleModal" tabindex="-1" role="dialog" aria-labelledby="sysUserModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="insertexampleModalLabel">添加出库信息</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    数据已添加!
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary btn-sureDelStock">确定</button>
                </div>
            </div>
        </div>
    </div>
</section>
<script>
    var arr = [];

    $("#addtable").click(function () {

        if ($("#AppendID").val() == "") {
            alert('请输入订单号');
            return false;
        }
        if ($("#CreateDate").val() == "") {
            alert('请输入制单日期');
            return false;
        }
        if ($("#TradeDate").val() == "") {
            alert('请输入入库日期');
            return false;
        }
        if ($("#AlreadyPay").val() == "") {
            alert('请输入已经收款');
            return false;
        }
        if ($("#Description").val() == "") {
            alert('请输入备注');
            return false;
        }

        //$(this).toggleClass("table_none").siblings(".btn-success").toggleClass("table_none");

        $("#tables").removeClass("table_none");
    });

    $("#updatetable").click(function () {

        $("#tables").removeClass("table_none");

    })
    $.post('/Store/ProductsList', function (res) {
        $("#ProductsName").empty();
        $.each(res, function (index, item) {
            $("#ProductsName").append('<option >' + item.ProductsName + '</option>');
        })
    })

    $.post('/Store/supplierList', function (res) {
        $("#SupplierName").empty();
        $.each(res, function (index, item) {
            $("#SupplierName").append('<option >' + item.SupplierName + '</option>');
        })
    })

    //明细表单提交
    function ajaxSubmit() {
        //订单编号
        var NAppendID = document.getElementById("AppendID");
        //商品
        var NProductsID = document.getElementById("ProductsName");
        //数量
        var NProductsNum = document.getElementById("Quantity");
        //价格
        var NPrice = document.getElementById("Price");
        //供应商
        var NSupplierName = document.getElementById("SupplierName");
        //描述
        var NDescriptions = document.getElementById("Descriptions");
        //采购额
        var NAlreadyPay = document.getElementById("AlreadyPay");

        if (NProductsID.value == "") {
            alert('请选择商品');
            return false;
        }
        if (NProductsNum.value == "") {
            // alert("请输入商品数量");
            alert('请输入商品数量');
            return false;
        }
        if (!/^\d+$/.test(NProductsNum.value)) {
            alert("商品数量应该是整数");

            return false;
        }
        if (NProductsNum.value < 1) {
            NProductsNum.value = 0;
            alert("商品数量不应该小于1");
            return false;
        }
        if (!/^(-|\+)?\d+(\.\d+)?$/.test(NPrice.value)) {

            alert('商品价格输入有误');
            return false;
        }
        if (NPrice.value < 0) {
            alert('商品价格不能是负的');
            return false;
        }

        var list = {
            AppendID: $("#AppendID").val(),
            ProductsName: $("#ProductsName").val(),
            SupplierName: $("#SupplierName").val(),
            TotalSalesNum: $("#AlreadyPay").val(),
            Cost: $("#Quantity").val(),
            Price: $("#Price").val()
        }
        console.log(list);

        arr.push(list);
    }

    ///明细表单提交后，清空输入框中的值
    function ajaxSubmited() {
        $("#AppendID").val("");
        $("#ProductsName").val("");
        $("#SupplierName").val("");
        $("#AlreadyPay").val("");
        $("#Quantity").val("");
        $("#Price").val("");
        $("#CreateDate").val("");
        $("#TradeDate").val("");
        $("#AlreadyPay").val("");
        $("#Description").val("");
    }

    Array.prototype.remove = function (val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };

    function isInArray(arr, value) {
        for (var i = 0; i < arr.length; i++) {
            if (value === arr[i]) {
                return true;
            }
        }
        return false;
    }
    //遍历表格
    function errlist() {
        $.each(arr, function (index, item) {
            $("#ol").append('<tr><td>' + item.AppendID + '</td><td>' + item.ProductsName + '</td><td>' + item.SupplierName + '</td><td>' + item.TotalSalesNum + '</td><td>' + item.Cost + '</td><td>' + item.Price + '</td><td><button class="btn btn-sm btn-danger btn-deletes" onclick="retemet(' + index + ')"><i class="icon SkyUI icon-close"></i>删除</button></td></tr>');
        })
    }

    //保存
    $("#baocun").click(function () {

        //判断输入的AppendID是否存在
        ajaxSubmit();

        $("#ol").html("");

        errlist();

        //清空数据
        ajaxSubmited();

        //$("#addtable").toggleClass("table_none").siblings(".btn-success").toggleClass("table_none");

        $("#tables").addClass("table_none");

    })

    //提交表单

    $("#addetable").click(function () {
        if (confirm('您确定要完成吗！确认后不能修改')) {
            if (arr.length > 0) {
                finishAll();
            } else {
                alert("请生成表单数据")
            }
        }
    });

    function finishAll() {
        alert("提交成功")
        $tab.reloadTab('#OutStackModule');
    }

    function retemet(id) {

        $("#ol").html("");
        arr.splice(id, 1);
        errlist();

    }


    //;
    //!function () {
    //    //定义三个变量进去存储（id,确定按钮，删除）
    //    var currentId = 0;
    //    var $btnSureDelStock = $('.btn-sureDelStock');
    //    var $btnDelStock = $('.btn-DelStock');
    //    $btnDelStock.on('click', function () {
    //        currentId = $btnDelStock.attr('data-id');
    //    });
    //    //删除方式
    //    $btnSureDelStock.on('click', function () {
    //        $.ajax({
    //            url: '/store/OutStockModule',
    //            type: 'DELETE',
    //            dataType: 'JSON',
    //            data: { id: currentId },
    //            success: function (data) {
    //                $('.modal').modal('hide');
    //                $('#delAppendStockModuleModal').modal('show');
    //                $tab.reloadTab('#OutStackModule');
    //            }
    //        });
    //    });
    //    //增加
    //    $('.appendStock_Frm').submit(function (e) {
    //        e.preventDefault();
    //        $('.appendStock_Frm').ajaxSubmit(function (e) {
    //            $.ajax({
    //                url: '/store/OutStockModule',
    //                type: 'POST',
    //                success: function (data) {
    //                    $('#AddAppendStockInfoModuleModal').modal('show');
    //                }
    //            });
    //        });
    //    });
    //}();
</script>
